<%-- 
    Document   : SearchResult
    Created on : Mar 8, 2012, 3:09:36 AM
    Author     : maconcon
--%>
<%@ page import="java.sql.*" %>
<%@ page import="java.io.*" %>
<%@ page import="java.lang.Integer" %>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
        <%
            response.setHeader("Pragma","no-cache");
            response.setHeader("Cache-Control","no-store");
            response.setHeader("Expires","0");
            response.setDateHeader("Expires",-1);
        %>
        
           <%
        if(session.getAttribute("user")== null){
            response.sendRedirect("index.jsp");
            }

               else{
        %>
        
    </head>
    <body>
       <%
   String name = request.getParameter("search");
   session.setAttribute( "search", name );
%>
      <%
    try {

String connectionURL = "jdbc:mysql://localhost:3306/databasefoobar";
// declare a connection by using Connection interface
Connection connection = null;
/* declare object of Statement interface that is used for executing sql
statements. */
Statement statement = null;
// declare a resultset that uses as a table for output data from tha table.
ResultSet rs = null;
ResultSet rs2= null;
// Load JBBC driver "com.mysql.jdbc.Driver"
Class.forName("com.mysql.jdbc.Driver").newInstance();
/* Create a connection by using getConnection() method that takes parameters
of string type connection url, user name and password to connect to database.*/
connection = DriverManager.getConnection(connectionURL, "root", "123456");
/* createStatement() is used for create statement object that is used for
sending sql statements to the specified database. */
int x=Integer.parseInt(session.getAttribute("id").toString());
String 	add=	session.getAttribute("address").toString();
String city=		session.getAttribute("city").toString();
String post=	session.getAttribute("postal").toString();

String query1="select tempcart.productid, products.productid,products.title,products.synopsis,products.price,products.type, tempcart.customerid from tempcart,products where tempcart.customerid=";
statement = connection.createStatement();
         PreparedStatement pstmt = connection.prepareStatement("select tempcart.cartid, tempcart.productid, tempcart.quantity,products.productid,products.title,products.synopsis,products.price,products.type, tempcart.customerid from tempcart,products where tempcart.customerid=? AND tempcart.productid=products.productid ");
// PreparedStatement pstmt = connection.prepareStatement("select title,synopsis,price,type,quantity,cartid from products"+
//"inner join tempcart on tempcart.productid=products.productid");
pstmt.setInt(1, x);

 rs = pstmt.executeQuery();

 int price=0;

// sql query to retrieve values from the secified table.
%>

 <a href="BrowseProducts.jsp"> Browse Products </af>
        <a href="customerprofile.jsp"> View Profile </af>
              <a href="customer.jsp"> Home </af>
         <a href="UserLogoutServlet"> Logout </a>

  <form action="SearchResult.jsp" method="POST">
            Search Products: <input type="text" name="search" /><input type="submit" value="Search" />
        </form>
      <% out.print("  " +session.getAttribute("id")+ "");%>
      <% out.print("Welcome " +session.getAttribute("user")+ "");%>
 <TABLE BORDER="1">
            <TR>
            
                <TH>Title</TH>
                <TH>Synopsis</TH>
                <TH>Type</TH>
                <TH>Price</TH>
                <TH>Quantity</TH>
          
                          <TH>Update</TH>
                          <TH>Remove</TH>
            </TR>
            <% while(rs.next()){ %>
            <TR>
               
               <%=price+=(rs.getInt("products.price")* rs.getInt("tempcart.quantity"))%>
            
                <TD> <%= rs.getString("products.title") %></TD>
                <TD> <%= rs.getString("products.synopsis") %></TD>
                 <TD> <%= rs.getString("products.type") %> </TD>
                      <TD> <%= rs.getInt("products.price") %>   x</td>
                
                            <form action="UpdateCart1" method="post">
                   <TD><input type="text" name="quantity"  value=<%= rs.getString("tempcart.quantity") %> size="2"></TD>
                    <input type="hidden" name="cartid"  value=<%= rs.getString("tempcart.cartid") %>>
                       <TD> <input type="submit" value="Update" /></TD></form>
                  <form action="DeleteCart1" method="post"> 
                      <input type="hidden" name="cartid"  value=<%= rs.getString("tempcart.cartid") %>>
                      <TD> <input type="submit" value="Remove" /></TD>
                  
          </form>  </TR>
            <% } %>
        </TABLE>
        
    <font size="3">Total: <%=price%>   </font>
    <% /*String 	add=	session.getAttribute("address").toString();
String city=		session.getAttribute("city").toString();
String post=	session.getAttribute("postal").toString();*/%>
      
            
              <form action="CheckoutServlet1" method="POST">
            <input type="hidden" name="cost" value= <%=price%> />     
            <input type="hidden" name="customerid" value=<%=x%> />  
            
            <input type="submit" value="Checkout" />
            
<%

// close all the connections.
rs.close();
statement.close();
connection.close();
} catch (Exception ex) {
%>
</font>
<font size="+3" color="red"></b>
<%
out.println("Unable to connect to database.");
}
%> 
        
    </body>
    <%
    }
    %>
</html>
